package com.netintech.service;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class Decoder extends ReplayingDecoder<Void> {
    /**
     * @Author changjianbao
     * @Description 解码器处理粘包；根据消息长度和消息头A2进行拆包；
     *              首次接收出现半包则直接丢掉半包内容；只接受A2开头的消息；读取A2后续的消息内容存入Person.setBody(bytes);
     * @Date 2019/8/6 13:50
     * @Param [channelHandlerContext, byteBuf, list]
     * @return void
     **/
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] heartbeats = new byte[1];
        byteBuf.readBytes(heartbeats);
        int len = 0;
        String s= new String(heartbeats,"UTF-8");
        Person p = new Person();
        if(heartbeats[0] == -94){//普通消息
            len = 18;
            byte[] bytes = new byte[len];
            byteBuf.readBytes(bytes);
            p.setLength(len);
            p.setBody(bytes);
            list.add(p);
        }
    }
}
